import { defineStore } from "pinia";
import axios from "axios";
import { reactive } from "vue";

const useQuestionStore = defineStore("question", () => {
   const state = reactive({
    questionList: [], //问题列表
    answerList: [],//答案列表
    num:0,//当前题目序号
    
   })

   function getQuestionList() {
    //获取题目
        axios.get('https://mock.mengxuegu.com/mock/6767738f98077B17fe6792e2/question-naire')
        .then((res) => {
            console.log(res);
            state.questionList = res.data.questions;
            
        })
   }


   function saveAnswerList(answer) {
        state.answerList.push(answer);
    
   }
   function addNum() {
        state.num++;
   }
   function calculateScore() {
        let score = 0;
        state.answerList.forEach((answer,index) =>  {
            if(answer === 1 ) {
                score += 20;
            }
        })
        return score;
   }
    return { 
        state, 
        getQuestionList,
        saveAnswerList,
        addNum,
        calculateScore
    };
})

export default useQuestionStore;